#include "class-engine.h"
#include "class-assets.h"

/***
 * cAsset Member Functions
 **/

// Constructor
cAsset::cAsset(){

	// Set the default values for the member fields

	assetName = "Empty";

	width  = 0;
	height = 0;
	depth  = 0;
	z      = 0;

	handleX = 0;
	handleY = 0;
}

// Destructor
cAsset::~cAsset(){

	// Delete the OpenGL Texture
	glDeleteTextures(1, &texture);

	// Delete the SDL Surface
	SDL_FreeSurface(surface);
}

void cAsset::loadAsset(string filename){
	
	SDL_Surface *tmp;

    if (tmp = IMG_Load(filename.c_str())){ 

		surface = SDL_DisplayFormatAlpha(tmp);
  
		width  = surface->w; // Set the texture width
		height = surface->h; // Set the texture height 

		// Set the texture depth
		depth  = surface->format->BitsPerPixel; 

		// Set the handle for the texture as the center
		handleX = width/2;	
		handleY = height/2;
        
        glGenTextures(1, &texture);				// Open a OpenGL Texture Handle        
        glBindTexture(GL_TEXTURE_2D, texture);	// Bind the texture object
        
        // Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        
        // Edit the texture object's image data using the information SDL_Surface gives us
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA,	GL_UNSIGNED_BYTE, surface->pixels);
    
	}else{

		// Report a failure and die
		cout << endl;
		cout << "-------------------------------------------------------------------------------" << endl;
		cout << "Error: " << __FUNCTION__ << " - " << SDL_GetError() << endl;
		cout << "-------------------------------------------------------------------------------" << endl;
		cout << endl;
		system("dir");
		cin.get();
		exit(1);
    }  
}


/***
 * cEntity Member Functions
 **/

cEntity::cEntity(){

	// Set the default values for the member fields

	x = 0;
	y = 0;
	h = 0;
	z = 0;

	angle = 1;

	timestamp = SDL_GetTicks();

	pixelsPerSecond = 5;

	movementVector.angle    = 0;
	movementVector.velocity = 0;
}

void cEntity::logic(){

}

void cEntity::render(){


	// Rotate the Top Left Corner
	topLeft.x			= (float)(0 - assets[0]->handleX);
	topLeft.y			= (float)(0 - assets[0]->handleY);	

	topLeft.velocity	= sqrt((topLeft.x*topLeft.x)+(topLeft.y*topLeft.y)); 
	topLeft.angle		= atan2(topLeft.y,topLeft.x); 

	topLeft.angle		= topLeft.angle + angle;
	
	topLeft.x			= (float)(x + topLeft.velocity*cos(topLeft.angle));
	topLeft.y			= (float)(y + topLeft.velocity*sin(topLeft.angle));

	// Rotate the Top Right Corner
	topRight.x			= (float)assets[0]->handleX;
	topRight.y			= (float)(0 - assets[0]->handleY);
	
	topRight.velocity	= sqrt((topRight.x*topRight.x)+(topRight.y*topRight.y)); 
	topRight.angle		= atan2(topRight.y,topRight.x); 

	topRight.angle		= topRight.angle + angle;
	
	topRight.x			= (float)(x + topRight.velocity*cos(topRight.angle));
	topRight.y			= (float)(y + topRight.velocity*sin(topRight.angle));
	
	// Rotate the Bottom Right Corner
	bottomRight.x			= (float)assets[0]->handleX;
	bottomRight.y			= (float)assets[0]->handleX;
	
	bottomRight.velocity	= sqrt((bottomRight.x*bottomRight.x)+(bottomRight.y*bottomRight.y)); 
	bottomRight.angle		= atan2(bottomRight.y,bottomRight.x); 

	bottomRight.angle		= bottomRight.angle + angle;
	
	bottomRight.x			= x + bottomRight.velocity*cos(bottomRight.angle);
	bottomRight.y			= y + bottomRight.velocity*sin(bottomRight.angle);

	// Rotate the Bottom Left Corner
	bottomLeft.x		= (float)(0 - assets[0]->handleX);
	bottomLeft.y		= (float)assets[0]->handleY;
	
	bottomLeft.velocity = sqrt((bottomLeft.x*bottomLeft.x)+(bottomLeft.y*bottomLeft.y)); 
	bottomLeft.angle    = atan2(bottomLeft.y,bottomLeft.x); 

	bottomLeft.angle	= bottomLeft.angle + angle;
	
	bottomLeft.x		= x + bottomLeft.velocity*cos(bottomLeft.angle);
	bottomLeft.y		= y + bottomLeft.velocity*sin(bottomLeft.angle);

	// Bind the texture to which subsequent calls refer to
	glBindTexture(GL_TEXTURE_2D, assets[0]->texture);
	
	glBegin(GL_QUADS);

		glTexCoord2i(0, 0);
		glVertex2f(topLeft.x, topLeft.y);
    
		glTexCoord2i(1, 0);
		glVertex2f(topRight.x, topRight.y);
    
		glTexCoord2i(1, 1);
		glVertex2f(bottomRight.x, bottomRight.y);
    
		glTexCoord2i(0, 1);
		glVertex2f(bottomLeft.x, bottomLeft.y);

	glEnd();
}